﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 分卷数据操作
    /// 
    /// 创建: 邱柯皓 2009/02/04 15:01
    /// </summary>
    public class Volume
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Volume() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取分卷实体对象
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:06
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回分卷实体对象</returns>
        private Yom.Model.Volume GetModel(SqlDataReader dr)
        {
            Yom.Model.Volume model = new Yom.Model.Volume();
            model.ComicID = db.GetLong(dr["ComicID"]);
            model.OrderID = db.GetInt(dr["OrderID"]);
            model.VolumeID = db.GetLong(dr["VolumeID"]);
            model.VolumeName = db.GetString(dr["VolumeName"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取分卷列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:08
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回分卷列表</returns>
        private List<Yom.Model.Volume> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Volume> list = new List<Yom.Model.Volume>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加分卷
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:09
        /// </summary>
        /// <param name="model">分卷实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Volume model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Volume] (");
            strSQL.Append("[ComicID], [VolumeName], [OrderID]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@ComicID, @VolumeName, @OrderID)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@ComicID", DbType.Int64, model.ComicID),
                db.CreateInSqlParameter("@VolumeName", DbType.String, model.VolumeName),
                db.CreateInSqlParameter("@OrderID", DbType.Int32, model.OrderID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改分卷
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:12
        /// </summary>
        /// <param name="model">分卷实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Volume model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Volume] SET ");
            strSQL.Append("[ComicID]=@ComicID, [VolumeName]=@VolumeName, ");
            strSQL.Append("[OrderID]=@OrderID ");
            strSQL.Append("WHERE [VolumeID]=@VolumeID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@ComicID", DbType.Int64, model.ComicID),
                db.CreateInSqlParameter("@VolumeName", DbType.String, model.VolumeName),
                db.CreateInSqlParameter("@OrderID", DbType.Int32, model.OrderID),
                db.CreateInSqlParameter("@VolumeID", DbType.Int64, model.VolumeID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除分卷
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:15
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(long VolumeID)
        {
            string strSQL = "DELETE FROM [Yom_Volume] WHERE [VolumeID]=@VolumeID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@VolumeID", DbType.Int64, VolumeID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL, cmdParms) > 0;
        }

        /// <summary>
        /// 获取分卷实体对象
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:19
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>返回分卷实体对象</returns>
        public Yom.Model.Volume GetModel(long VolumeID)
        {
            string strSQL = "SELECT * FROM [Yom_Volume] WHERE [VolumeID]=@VolumeID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@VolumeID", DbType.Int64, VolumeID) };

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, cmdParms))
            {
                Yom.Model.Volume model = new Yom.Model.Volume();

                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 分页获取分卷列表
        /// 
        /// 创建: 邱柯皓 2009/03/10 12:15
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="currPage">当前页索引</param>
        /// <param name="condition">查询条件</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回分卷列表</returns>
        public List<Yom.Model.Volume> GetList(int pageSize, int currPage, string condition, bool sort)
        {
            return GetList(pageSize, currPage, 0, condition, sort);
        }

        /// <summary>
        /// 分页获取指定漫画下的分卷列表
        /// 
        /// 创建: 邱柯皓 2009/03/10 12:17
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="currPage">当前页索引</param>
        /// <param name="ComicID">漫画编号</param>
        /// <param name="condition">查询条件</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回分卷列表</returns>
        public List<Yom.Model.Volume> GetList(int pageSize, int currPage, long ComicID, string condition, bool sort)
        {
            string sql = condition;
            if (ComicID != 0 && !string.IsNullOrEmpty(condition))
                sql += " AND [ComicID]=" + ComicID.ToString();
            else if (ComicID != 0 && string.IsNullOrEmpty(condition))
                sql = "[ComicID]=" + ComicID.ToString();
            using (SqlDataReader dr = db.GetPager(pageSize, currPage, "[Yom_Volume]", "[OrderID]", sql, sort))
            {
                List<Yom.Model.Volume> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 获取指定漫画下的分卷列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:16
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回分卷列表</returns>
        public List<Yom.Model.Volume> GetList(long ComicID)
        {
            string strSQL = "SELECT * FROM [Yom_Volume] WHERE [ComicID]=@ComicID ORDER BY [OrderID] ASC";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@ComicID", DbType.Int64, ComicID) };

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, cmdParms))
            {
                List<Yom.Model.Volume> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 设置分卷排序
        /// 
        /// 创建: 邱柯皓 2009/02/04 15:23
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <param name="OrderID">排序编号</param>
        /// <returns>返回成功标识</returns>
        public bool SetOrder(long VolumeID, int OrderID)
        {
            Yom.Model.Volume model = GetModel(VolumeID);
            model.OrderID = OrderID;

            return Update(model);
        }

        /// <summary>
        /// 获取最后分卷实体对象
        /// 
        /// 创建: 邱柯皓 2009/03/09 13:28
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回最后分卷实体对象</returns>
        public Yom.Model.Volume GetLastVolume(long ComicID)
        {
            string strSQL = "SELECT TOP 1 * FROM [Yom_Volume] WHERE [ComicID]=" + ComicID.ToString() + " ORDER BY [VolumeID] DESC";
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Volume model = new Yom.Model.Volume();
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取上一分卷
        /// 
        /// 创建: 邱柯皓 2009/03/09 09:23
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>返回分卷实体对象</returns>
        public Yom.Model.Volume GetPreVolume(long VolumeID)
        {
            string strSQL = "SELECT TOP 1 * FROM [Yom_Volume] WHERE [VolumeID]<" + VolumeID + " ORDER BY [VolumeID] DESC";
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Volume model = new Yom.Model.Volume();
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取下一分卷
        /// 
        /// 创建: 邱柯皓 2009/03/09 09:27
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>获取下一分卷实体对象</returns>
        public Yom.Model.Volume GetNextVolume(long VolumeID)
        {
            string strSQL = "SELECT TOP 1 * FROM [Yom_Volume] WHERE [VolumeID]>" + VolumeID + " ORDER BY [VolumeID] ASC";
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Volume model = new Yom.Model.Volume();
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取漫画下的分卷数量
        /// 
        /// 创建: 邱柯皓 邱柯皓 2009/03/10 12:29
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <returns>返回分卷数量</returns>
        public int GetCount(long ComicID)
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Volume] WHERE [ComicID]=" + ComicID.ToString();
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);
            return db.GetInt(m);
        }
        #endregion
    }
}